﻿using Customer.API.Applcation.Command.FinanceCommand;
using Customer.API.Dtos;
using Customer.Domain;
using Customer.Domain.Dto;
using Customer.Infrstructre.FinanceManager;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Customer.API.Controllers
{
    /// <summary>
    /// 财务管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class FinanceTypeController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public FinanceTypeController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加财务类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddFinanceType(ADDFinanceCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.AddName = name;
                result.Data=await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改财务类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdFinanceType(UpdFinanceCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.UpdName = name;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除财务类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelFinanceType(DelFinanceCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.DelName = name;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }


        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        ///////////////////////////财务应收//////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 添加财务应收管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddNewPaid(ReceivableDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                command.ResidueMoney = command.ReceivableAmount;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.AddName = name;
                result.Data = await _mediator.Send(new ADDReceivableCommand { dto= command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改财务应收
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdNewPaid(ReceivableUpdDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                //获取用户名
                var name= HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.UpdName = name;
                result.Data = await _mediator.Send(new UpdReceivableCommand {dto= command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除财务应收
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelNewPaid(DelReceivableCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.DelName = name;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加财务实收管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPay(ADDPaymentCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.User_Id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Office_Id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.AddName= name;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }














    }
}
